using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Globalization;

namespace SBPweb.Common.Diagnostics
{
	/// <summary>
	/// Class of hashing
	/// </summary>
	public static class Hashing
	{

#if !SILVERLIGHT
        /// <summary>
		/// Get MD5 hash
		/// </summary>
		/// <param name="bytes">bytes</param>
		/// <returns>hash</returns>
		public static string GetMD5HashStringOfBytes(byte[] bytes)
		{
			MD5 md5 = new MD5CryptoServiceProvider();
			byte[] result = md5.ComputeHash(bytes);

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < result.Length; i++)
			{
				sb.Append(result[i].ToString("X2", CultureInfo.CurrentCulture));
			}
			return sb.ToString();
		}

		/// <summary>
		/// Convert long to hex
		/// </summary>
		/// <param name="l">long</param>
		/// <returns>string</returns>
		public static string LongToHex16(long l)
		{
			return l.ToString("x16", CultureInfo.CurrentCulture);
		}

		/// <summary>
		/// Get MD5 hash
		/// </summary>
		/// <param name="fileInfo">file</param>
		/// <returns>hash</returns>
		public static string GetMD5HashStringOfBytes(System.IO.FileInfo fileInfo)
		{
			FileStream fs = fileInfo.OpenRead();
			string retVal = GetMD5HashStringOfBytes(fs);
			fs.Close();
			return retVal;
		}

		/// <summary>
		/// Get MD5 hash
		/// </summary>
		/// <param name="stream">stream</param>
		/// <returns>hash</returns>
		public static string GetMD5HashStringOfBytes(Stream stream)
		{
			BinaryReader br = new BinaryReader(stream);
			byte [] bytes = br.ReadBytes((int)(stream.Length));
			br.Close();
			return GetMD5HashStringOfBytes(bytes);
		}

#endif

        /// <summary>
        /// Get SHA1 hash
        /// </summary>
        /// <param name="bytes">bytes</param>
        /// <returns>hash</returns>
        public static string GetSHA1HashStringOfBytes(byte[] bytes)
        {
            SHA1 sha = new SHA1Managed();
            byte[] result = sha.ComputeHash(bytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("X2", CultureInfo.CurrentCulture));
            }
            return sb.ToString();
        }

        /// <summary>
        /// Get MD5 hash
        /// </summary>
        /// <param name="fileInfo">file</param>
        /// <returns>hash</returns>
        public static string GetSHA1HashStringOfBytes(System.IO.FileInfo fileInfo)
        {
            FileStream fs = fileInfo.OpenRead();
            string retVal = GetSHA1HashStringOfBytes(fs);
            fs.Close();
            return retVal;
        }

        /// <summary>
        /// Get MD5 hash
        /// </summary>
        /// <param name="stream">stream</param>
        /// <returns>hash</returns>
        public static string GetSHA1HashStringOfBytes(Stream stream)
        {
            BinaryReader br = new BinaryReader(stream);
            byte[] bytes = br.ReadBytes((int)(stream.Length));
            br.Close();
            return GetSHA1HashStringOfBytes(bytes);
        }

    }

}
